Programming Persistent Memory by Steve Scargall

Programming Persistent Memory by Steve Scargall
Author: Steve Scargall
Language: eng
Format: epub, pdf
ISBN: 9781484249321
Publisher: Apress

Nonpersistent Stores


Nonpersistent stores refer to data written to persistent memory but not flushed explicitly. It is understood that if the program writes to persistent memory, it wishes for those writes to be persistent. If the program ends without explicitly flushing writes, there is an open possibility for data corruption. When a program exits gracefully, all the pending writes in the CPU caches are flushed automatically. However, if the program were to crash unexpectedly, writes still residing in the CPU caches could be lost.

Consider the code in Listing 12-7 that writes data to a persistent memory device mounted to /mnt/pmem without flushing the data. 32 #include <stdio.h>

33 #include <sys/mman.h>

34 #include <fcntl.h>

35

36 int main(int argc, char *argv[]) {

37 int fd, *data;

38 fd = open("/mnt/pmem/file", O_CREAT|O_RDWR, 0666);

39 posix_fallocate(fd, 0, sizeof(int));

40 data = (int *) mmap(NULL, sizeof(int), PROT_READ |

41 PROT_WRITE, MAP_SHARED_VALIDATE |

42 MAP_SYNC, fd, 0);

43 *data = 1234;

44 munmap(data, sizeof(int));

45 return 0;

46 }